﻿using MediatR;
using Microsoft.Extensions.Configuration;
using Traceability.APP.Application.Command.Traceablitys;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Traceablitys
{
    public class LoginCommandHandLer : IRequestHandler<LoginCommand, APIResult<Account>>
    {

        private readonly IBaseRepostiy<Account> account;
        public LoginCommandHandLer(IBaseRepostiy<Account> account)
        {
            this.account = account;

        }
        public Task<APIResult<Account>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {

            APIResult<Account> result = new APIResult<Account>();
            var user = account.GetAll().FirstOrDefault(x => x.AccountName == request.AccountName && x.PassWord == request.PassWord);
            if (user != null)
            {


                result.Code = ResultCode.Ok;
                result.Data = user;
                result.Message = "登录成功";
            }
            else
            {
                result.Code = ResultCode.Fail;
                result.Message = "账号和密码错误";
            }
            return Task.FromResult(result);
        }
    }
}
